package com.shuwang.goods.controller;

import com.shuwang.goods.exception.ConflictException;
import com.shuwang.goods.exception.ForbiddenException;
import com.shuwang.goods.exception.NotFoundException;
import com.shuwang.goods.mapper.GoodsCategoryRelativeMapper;
import com.shuwang.goods.model.GoodsCategoryRelative;
import com.shuwang.goods.model.GoodsCategoryRelativeExample;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by Administrator on 2017/6/5.
 */
@RestController
@RequestMapping(value = "goods")
@Api(value = "goodscategoryrelative",description = "商品分类关联信息")
public class GoodsCategoryRelativeController {

    protected final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private GoodsCategoryRelativeMapper goodsCategoryRelativeMapper;

    @RequestMapping(value = "categories", method = RequestMethod.POST)
    @ApiOperation(value = "商品分类关联信息接口")
    public List<GoodsCategoryRelative> createGoodsCategoryRelative(@ApiParam(value = "商品分类关联信息实体类") @RequestBody GoodsCategoryRelative model) {

        GoodsCategoryRelativeExample goodsCategoryRelativeExample =new GoodsCategoryRelativeExample();
        goodsCategoryRelativeExample.createCriteria().andCategoryIdEqualTo(model.getCategoryId()).andGoodsIdEqualTo(model.getGoodsId());
        if(!goodsCategoryRelativeMapper.selectByExample(goodsCategoryRelativeExample).isEmpty()){
            log.debug("该商品分类关联信息已存在");
            throw new ConflictException("该商品分类关联信息已存在");
        }

        GoodsCategoryRelative goodsCategoryRelative = new GoodsCategoryRelative();
        BeanUtils.copyProperties(model,goodsCategoryRelative);
        goodsCategoryRelative.setGoodsId(model.getGoodsId());
        goodsCategoryRelativeMapper.insertSelective(goodsCategoryRelative);

        List<GoodsCategoryRelative> success = goodsCategoryRelativeMapper.selectByExample(goodsCategoryRelativeExample);
        if(success.isEmpty()){
            log.debug("商品分类关联信息入库失败");
            throw new NotFoundException("商品分类关联信息入库失败");
        }
        return success;
    }

    @RequestMapping(value = "getcategory/{categoryId}",method = {RequestMethod.GET})
    @ApiOperation(value = "根据categoryId查询商品分类关联信息", httpMethod = "", response = GoodsCategoryRelative.class)
    public List<GoodsCategoryRelative> getGoodsCategoryRelativeByCategoryIdController(@ApiParam(value = "请输入categoryId") @PathVariable Long categoryId ) {

        GoodsCategoryRelativeExample goodsCategoryRelativeExample =new GoodsCategoryRelativeExample();
        goodsCategoryRelativeExample.createCriteria().andCategoryIdEqualTo(categoryId);
        List<GoodsCategoryRelative> goodsCategoryRelative =goodsCategoryRelativeMapper.selectByExample(goodsCategoryRelativeExample);

        if( goodsCategoryRelative.isEmpty()){
            log.debug("该商品分类关联信息不存在");
            throw new NotFoundException("该商品分类关联信息不存在");
        }
        return goodsCategoryRelative;
    }

    @RequestMapping(value = "getgoods/{goodsId}",method = {RequestMethod.GET})
    @ApiOperation(value = "根据goodsId查询商品分类关联信息", httpMethod = "", response = GoodsCategoryRelative.class)
    public List<GoodsCategoryRelative> getGoodsCategoryRelativeBGoodsIdController(@ApiParam(value = "请输入goodsId") @PathVariable Long goodsId ) {

        GoodsCategoryRelativeExample goodsCategoryRelativeExample =new GoodsCategoryRelativeExample();
        goodsCategoryRelativeExample.createCriteria().andGoodsIdEqualTo(goodsId);
        List<GoodsCategoryRelative> goodsCategoryRelative =goodsCategoryRelativeMapper.selectByExample(goodsCategoryRelativeExample);

        if( goodsCategoryRelative.isEmpty()){
            log.debug("该商品分类关联信息不存在");
            throw new NotFoundException("该商品分类关联信息不存在");
        }
        return goodsCategoryRelative;
    }

    @RequestMapping(value = "deletecategory/{categoryId}",method = {RequestMethod.DELETE})
    @ApiOperation(value = "根据categoryId删除商品分类关联信息", httpMethod = "DELETE", response = GoodsCategoryRelative.class)
    public List<GoodsCategoryRelative>  deleteGoodsCategoryRelativeByCategoryIdController(@ApiParam(value = "请输入categoryId") @PathVariable Long categoryId ) {

        GoodsCategoryRelativeExample goodsCategoryRelativeExample = new GoodsCategoryRelativeExample();
        goodsCategoryRelativeExample.createCriteria().andCategoryIdEqualTo(categoryId);
        List<GoodsCategoryRelative> goodsCategoryRelative =goodsCategoryRelativeMapper.selectByExample(goodsCategoryRelativeExample);
        if (goodsCategoryRelative.isEmpty()) {
            log.debug("该商品分类关联信息不存在");
            throw new NotFoundException("该商品分类关联信息不存在");
        }
        goodsCategoryRelativeMapper.deleteByExample(goodsCategoryRelativeExample);

        if (!goodsCategoryRelativeMapper.selectByExample(goodsCategoryRelativeExample).isEmpty()) {
            log.debug("该商品分类关联信息删除失败");
            throw new ForbiddenException("该商品分类关联信息删除失败");

        }
        return goodsCategoryRelative;
    }

    @RequestMapping(value = "deletegoods/{goodsId}",method = {RequestMethod.DELETE})
    @ApiOperation(value = "根据goodsId删除商品分类关联信息", httpMethod = "DELETE", response = GoodsCategoryRelative.class)
    public List<GoodsCategoryRelative>  deleteGoodsCategoryRelativeByGoodsIdController(@ApiParam(value = "请输入goodsId") @PathVariable Long goodsId ) {

        GoodsCategoryRelativeExample goodsCategoryRelativeExample = new GoodsCategoryRelativeExample();
        goodsCategoryRelativeExample.createCriteria().andGoodsIdEqualTo(goodsId);
        List<GoodsCategoryRelative> goodsCategoryRelative =goodsCategoryRelativeMapper.selectByExample(goodsCategoryRelativeExample);
        if (goodsCategoryRelative.isEmpty()) {
            log.debug("该商品分类关联信息不存在");
            throw new NotFoundException("该商品分类关联信息不存在");
        }
        goodsCategoryRelativeMapper.deleteByExample(goodsCategoryRelativeExample);

        if (!goodsCategoryRelativeMapper.selectByExample(goodsCategoryRelativeExample).isEmpty()) {
            log.debug("该商品分类关联信息删除失败");
            throw new ForbiddenException("该商品分类关联信息删除失败");

        }
        return goodsCategoryRelative;
    }
}
